package com.kotlin.flong.base

import java.util.HashMap

/*
 * lambda表示
 */
fun main(args: Array<String>) {
    //groupBy()

    //minOrMax()

    //quchong()
    //listCf()
    //listSum()

    moreValOut()
}

//分组
fun groupBy() {
    var list1 = listOf("张三", "张思", "寅务")
    val map1 = list1.groupBy {
        var first = it.substring(0, 1)
        when (first) {
            "张" -> "张"
            "寅" -> "寅"
            else -> "其他"
        }
    }

    println(map1)

}

//排序
fun sort() {
    var list1 = listOf(Personx(29, "liangjl"), Personx(22, "梁继龙"), Personx(30, "寅务"))
    //sortedBy升序排序
    var list2 = list1.sortedBy { it.age }

    println(list2)

    //sortedByDescending倒序排序
    var list3 = list1.sortedByDescending { it.age }

    println(list3)

}

//求最小或最大值
fun minOrMax() {
    var list1 = listOf(Personx(29, "liangjl"), Personx(22, "梁继龙"), Personx(30, "寅务"))
    list1.maxByOrNull { it.age } //最大值
    list1.minByOrNull { it.age } //最小值
}


//去重复
fun quchong() {
    var list1 = listOf("张三", "张思", "寅务", "寅务", "茵曼")

    //val set	=	list1.toSet()

    //println(set)

    println(list1.distinctBy { it })

    //去掉同性的人名
    println(list1.distinctBy { it.substring(0, 1) })

}

//集合拆分
fun listCf() {

    var list1 = listOf("张三", "张思", "寅务", "寅务", "茵曼")

    var p = list1.partition { it.startsWith("张") }

    println(p.first)
    println(p.second)
}


fun listSum() {
    var list1 = listOf(Personx(29, "liangjl"), Personx(22, "梁继龙"), Personx(30, "寅务"))

    //所有人的年龄相加
    var sumAge = list1.sumBy { it.age }
    println(sumAge)
}


//可以多个值在一个大括号进行输出
fun moreValOut() {
    val (age, userName) = Personx(29, "liangjl")

    println(age)
    println(userName)
}

//创建一个实体类
data class Personx(var age: Int, var userName: String)

